package com.zb;

import org.springframework.core.io.ClassPathResource;
import org.springframework.security.jwt.Jwt;
import org.springframework.security.jwt.JwtHelper;
import org.springframework.security.jwt.crypto.sign.RsaSigner;
import org.springframework.security.jwt.crypto.sign.RsaVerifier;
import org.springframework.security.rsa.crypto.KeyStoreKeyFactory;

import java.security.KeyPair;
import java.security.interfaces.RSAPrivateKey;

public class TestJWT {
    public static void main(String[] args) {
        parse();
    }

    static void parse() {
        String token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzY29wZSI6WyJhcHAiXSwibmFtZSI6bnVsbCwiaWQiOm51bGwsImV4cCI6MTczMTM5NjM3OSwiYXV0aG9yaXRpZXMiOlsic2Vja2lsbF9saXN0IiwiZ29vZHNfbGlzdCJdLCJqdGkiOiIxYmYzNDJkOS1mZjczLTQ2N2UtYWFhOC0wNTc0YzdiZjE4YmEiLCJjbGllbnRfaWQiOiJ6aG9uZ2JvIiwidXNlcm5hbWUiOiJob25naGFpZXIifQ.ADLveoZXz3DsL_HDwYsiCJVK9CtHv536LGmWPwzj8YtMVeZgI2k9dfcGr1eP4UmFiKyzaBPkh31cq6_30BIBcJ2T1FaNrc9f31UlJ31kzthwPZzBawV_hTrlaLlvkxeqzeNY6dOLFSBciF8l94KQ5YuV-8uawLjjjbn0bHl2XYKnteh_SboCHybkV74JWERjplP3pQJXPW0dGDD29IO2yIJpADEkxhO6eR9Qveu0swSM9Qx8tjhOcrMKz2v2JV37kgwckM6J43opov21MH6UqD0i5-FpIAFqnTjRJgSoLN68n8msLPfrSoOEQ5D4eTT5vE6VkcAXDVvWwFOWQ91lhQ";
        String pubKey = "-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgDkhe3VEqoBG6wTMNSgrSM3GWXQreuN0h22eeg2H8ErQ0Tug0GYuWHFSo3rngt96Qj180Rx2R/cbk9FH+jQGa+h/t1al7ZYktLv72tR4z0NLR+5aOSWq/KB7JLDC7hykiV8Y/6G/d21igDD+4CxilJHayjng0HA7iLLVo4eDVmKjLcMVOfp7NKhTj6++LC4Xb5/UnDWqQHwNZxFrLWKd+BKcYwDhcpoIussCzvg6hftSeJq/OXc/LQNgueKLVAG8WUIhloY1fOe4NkrdXAs9wMUHfGn5EydcVt3zI/ISrW/Vv1JWnyJr1uvndW3bw/ZRx979MsbQ76rp6Ay4NuAGSwIDAQAB-----END PUBLIC KEY-----";

        Jwt jwt = JwtHelper.decodeAndVerify(token, new RsaVerifier(pubKey));
        System.out.println(jwt.getClaims());


    }


    static void create() {
        String key_location = "myshop.jks";
        //秘钥库密码
        String key_password = "myshop";
        //秘钥密码
        String keypwd = "myshop";
        //秘钥别名
        String alias = "myshop";

        ClassPathResource resource = new ClassPathResource(key_location);
        KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(resource, key_password.toCharArray());
        //密钥对象（公私）
        KeyPair keyPair = keyStoreKeyFactory.getKeyPair(alias, keypwd.toCharArray());
        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
        String username = "zhangsan";
        Jwt encode = JwtHelper.encode(username, new RsaSigner(privateKey));
        System.out.println(encode.getEncoded());
    }
}
